<?php

/* -------------------------------------- setup --------------------------------------- */

$cfg_pahToPHPrail = 'PHPrail'; # relative from this script or absolute

$cfg_mysqlDatabases = 'rail_winkler'; # mysql
$cfg_mysqlUser = 'root';
$cfg_mysqlPasswd = '';

$cfg_railPasswd = 'rail'; # rail root password

/* -------------------------------------- script ------------------------------------- */
require_once("$cfg_pahToPHPrail/rail_connector.class.php");
print( "rail_converter1.0to1.1
used databases: $cfg_mysqlDatabases

" );

// connect
try{
	$mysqli = new mysqli( 'localhost', $cfg_mysqlUser, $cfg_mysqlPasswd, $cfg_mysqlDatabases );
	$rail = new rail_connector( $cfg_mysqlDatabases, $cfg_mysqlUser, $cfg_mysqlPasswd );
	$rail->login('root', $cfg_railPasswd);
print( 'connected to rail..
' );

	// create objects_documents_data mysqltables
	$mysqli->query( "RENAME TABLE `$cfg_mysqlDatabases`.`objects_documents`  TO `$cfg_mysqlDatabases`.`old_objects_documents`" );
	$mysqli->query( "CREATE TABLE `objects_documents` (
					  `id` int(11) NOT NULL AUTO_INCREMENT,
					  `md5` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
					  `content` longblob NOT NULL,
					  PRIMARY KEY (`id`)
					) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci" );
print( 'create table objects_documents_data
' );

	// iterate ale rail_documents
print( '
content found:
' );
	$data = $mysqli->query( "SELECT `id` FROM `objects` WHERE `type`='RAIL_DOCUMENT' OR `type`='RAIL_PAGE'" );
	while( $data_row = $data->fetch_assoc() ){
		$rail_id = $data_row['id'];

		// ckeck DOCUMENT_MIMETYPE
		$data2 = $mysqli->query( "SELECT `mime-type` FROM old_objects_documents WHERE `object`='$rail_id' LIMIT 1" )->fetch_assoc();
		$mysqli->query( "INSERT INTO objects_attributes (`object`,`attribute`,`value`) VALUES ('$rail_id','DOCUMENT_MIMETYPE','" . $data2['mime-type'] . "')" );
		// check OBJ_DOCUMENT
		$data2 = $mysqli->query( "SELECT `value` FROM objects_attributes WHERE `object`='$rail_id' AND `attribute`='OBJ_DOCUMENT' LIMIT 1" )->fetch_assoc();
		if( count($data2) <= 0 )
			 $mysqli->query( "INSERT INTO objects_attributes (`object`,`attribute`,`value`) VALUES ('$rail_id','OBJ_DOCUMENT','')" );

		// set content
		$rdata = $mysqli->query( "SELECT `object`,`content` FROM old_objects_documents WHERE `object`='$rail_id' LIMIT 1" )->fetch_assoc();
		if( isset($rdata['content']) && strlen($rdata['content']) > 0 ){

			echo $rail_id . "hier";

				$object = rail_factory::get_object( $rail, $rail_id );
				$object->set_content($rdata['content']);

print( $object->get_name() . ' [' . strlen($rdata['content']) . ']
' );
		}
	}

	// add|update RAIL_VERSION
	$data = $mysqli->query( "SELECT `id` FROM `objects` WHERE `type`='RAIL_ROOT' LIMIT 1" )->fetch_assoc();
	$data2 = $mysqli->query( "SELECT `value` FROM objects_attributes WHERE `object`='" . $data['id'] . "' AND `attribute`='RAIL_VERSION' LIMIT 1" )->fetch_assoc();
	if( count($data2) <= 0 )
		$mysqli->query( "INSERT INTO objects_attributes (`object`,`attribute`,`value`) VALUES ('" . $data['id'] . "','RAIL_VERSION','1.1-" . PHPrail_REVISION . "')" );
	else
		$mysqli->query( "UDATE objects_attributes SET `value`='1.1-" . PHPrail_REVISION . "' WHERE `object`='" . $data['id'] . "' AND `attribute`='RAIL_VERSION' LIMIT 1" );

	// optimize all tables
	$mysqli->query( "DROP TABLE `old_objects_documents`");
	$mysqli->query( "OPTIMIZE TABLE `objects` , `objects_attributes` , `objects_documents` , `objects_documents_data` , `objects_relations`" );

print( '
completet.' );
}
catch( Exception $e ){
	print_r( '<!--' . $e . '-->' );
	die( 'converter failed.' );
}

?>